package susebs.pb.travel_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import susebs.pb.travel_server.domain.PageDto;
import susebs.pb.travel_server.entity.Hotels;
import susebs.pb.travel_server.entity.Pictures;
import susebs.pb.travel_server.dao.PicturesDao;
import susebs.pb.travel_server.service.PicturesService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Pictures)表服务实现类
 *
 * @author makejava
 * @since 2024-02-05 08:02:28
 */
@Service("picturesService")
public class PicturesServiceImpl implements PicturesService {
    @Resource
    private PicturesDao picturesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param picId 主键
     * @return 实例对象
     */
    @Override
    public Pictures queryById(Long picId) {
        return this.picturesDao.queryById(picId);
    }

    /**
     * 分页查询
     *
     * @param pictures    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Pictures> queryByPage(Pictures pictures, PageRequest pageRequest) {
        long total = this.picturesDao.count(pictures);
        return new PageImpl<>(this.picturesDao.queryAllByLimit(pictures, pageRequest), pageRequest, total);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo queryAllPicturesByPage(PageDto pageDto) {
        if(pageDto.getPage() == null || pageDto.getSize() == null){
            pageDto.setPage(1);
            pageDto.setSize(10);
        }
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Pictures> list = picturesDao.queryAllPictures(pageDto.getPictures());
        PageInfo<Pictures> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param pictures 实例对象
     * @return 实例对象
     */
    @Override
    public Pictures insert(Pictures pictures) {
        this.picturesDao.insert(pictures);
        return pictures;
    }

    /**
     * 修改数据
     *
     * @param pictures 实例对象
     * @return 实例对象
     */
    @Override
    public Pictures update(Pictures pictures) {
        this.picturesDao.update(pictures);
//        System.out.println("picservice:"+pictures);
        return this.queryById(pictures.getPicId());
    }

    /**
     * 通过主键删除数据
     *
     * @param picId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long picId) {
        return this.picturesDao.deleteById(picId) > 0;
    }

    /**
     * 请求某一类图片（单个/多个）
     * @param type 图片关联实体类型
     * @param id 图片关联实体id
     */
    public ArrayList<Pictures> queryPicturesByOwner(Integer type,Long id){
        Pictures params = new Pictures(null, null, type, id);
        ArrayList<Pictures> res = picturesDao.queryPicturesByOwner(params);
        if(res.isEmpty()){
            System.out.println("错误！没有图片！");
            return new ArrayList<>();
        }
        return res;
    }

    /**
     * 获取指定用户原始头像
     */
    public Pictures getUserOldPic(Long userId){
        return this.queryPicturesByOwner(4,userId).get(0);
    }
}
